package com.muqi.app.pj.shops;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.widget.MyListView;

public class MineOrderAdapter extends BaseAdapter {
    private List<OrderBean> mDataList;
    private Context mContext;
    private String addressType;
    private onPayDeleteListener mListener;

	public interface onPayDeleteListener {
		void onPay(int position);

		void onDuiHuan(int position);

		void onDelete(int position);

	}
    public void setCheckListener(onPayDeleteListener mCheckListener){
        this.mListener = mCheckListener;
    }



    public MineOrderAdapter(Context context, List<OrderBean> data){
        mContext = context;
        mDataList = data;
        this.addressType = addressType;
    }
    @Override
    public int getCount() {
        return mDataList==null?0:mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        if(mDataList==null || position>=mDataList.size()) return null;
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	final OrderBean data = mDataList.get(position);
        ViewHolder holder=null;
        if(null==convertView){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_mine_order,null);
            holder.orderBtn = (RelativeLayout)convertView.findViewById(R.id.mine_order);
            holder.order = (TextView)convertView.findViewById(R.id.order_num);
            holder.time = (TextView)convertView.findViewById(R.id.order_time);
            holder.state = (TextView)convertView.findViewById(R.id.order_state);
            holder.mListview = (MyListView)convertView.findViewById(R.id.mListview);
            holder.price = (TextView)convertView.findViewById(R.id.total_price);
            holder.deleteBtn = (TextView)convertView.findViewById(R.id.delete);
            holder.payBtn = (TextView)convertView.findViewById(R.id.pay);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
       
        holder.order.setText(data.getDingdanhao());
        holder.time.setText("下单时间:"+data.getCreatetime());
        if(data.getZhuangtai().equals("1")){
        	holder.state.setText("未支付");
        }else if(data.getZhuangtai().equals("2")){
        	holder.state.setText("已支付");
        }else if(data.getZhuangtai().equals("3")){
        	holder.state.setText("待发货");
        }else if(data.getZhuangtai().equals("4")){
        	holder.state.setText("已发货");
        }else if(data.getZhuangtai().equals("5")){
        	holder.state.setText("已完成");
        }
        
 
        	if("0".equals(data.getObj_type())){
        		DecimalFormat df = new DecimalFormat("0.00");
    			double x = Double.valueOf(data.getZongjia());
    			String result = df.format(x);	
        		holder.price.setText(result);
        		holder.payBtn.setText("去支付");
        	}else{
        		holder.price.setText((int)data.getZongjia()+"积分");
        		holder.payBtn.setText("去兑换");
        	}
        
        OrderGoodsAdapter goodsAdapter = new OrderGoodsAdapter(mContext, data.getDingdan_mx(),Integer.valueOf(data.getZhuangtai()),-1);
        holder.mListview.setAdapter(goodsAdapter);
        
        holder.payBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if("0".equals(data.getObj_type())){
					mListener.onPay(position);
				}else{
					mListener.onDuiHuan(position);
				}
			}
		});
        if(data.getZhuangtai().equals("1")){
        	holder.deleteBtn.setVisibility(View.VISIBLE);
        	holder.payBtn.setVisibility(View.VISIBLE);
        	holder.deleteBtn.setOnClickListener(new OnClickListener() {
        		@Override
        		public void onClick(View v) {
        			mListener.onDelete(position);
        		}
        	});
        }else{
        	holder.deleteBtn.setVisibility(View.GONE);
        	holder.payBtn.setVisibility(View.GONE);
        }
        return convertView;
    }
    class ViewHolder{
    	RelativeLayout orderBtn;
    	TextView order;
    	TextView time;
    	TextView state;
    	TextView price;
    	TextView payBtn;
    	TextView deleteBtn;
    	MyListView mListview;
    }
   
}
